﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimpleGUI.DetectionMethods.RandomDart
{
	class RandomDart: IDartDetector
	{
		/// <summary>
		/// New dart detected event
		/// </summary>
		public event DartDetectionEventHandler DartDetected;

		// some private stuff
		private Random r = new Random();
		private Timer timer = new Timer();
		private Setup setup;
		
		/// <summary>
		/// Has this class a interface?
		/// </summary>
		public bool hasInterface { get { return true; } }
		
		/// <summary>
		/// Is the interface visible?
		/// </summary>
		public bool visible
		{
			get
			{
				if(setup != null)
				{
					return setup.Visible;
				}
				return false;
			}
		}
		
		/// <summary>
		/// Constructor
		/// - initiates timer
		/// </summary>
		public RandomDart()
		{
			timer.Interval = 5000;
			timer.Tick += new EventHandler(timer_Tick);
		}

		/// <summary>
		/// Is the detection method running?
		/// </summary>
		public bool running
		{
			get { return timer.Enabled; }
		}

		/// <summary>
		/// Timer ticked, dart detected!
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void timer_Tick(object sender, EventArgs e)
		{
			detectDart();
		}
		
		/// <summary>
		/// Detect a dart
		/// </summary>
		private void detectDart()
		{
			// random shizzle!
			if (this.DartDetected != null)
			{
				this.DartDetected(this, r.Next(180));
			}
		}
		
		/// <summary>
		/// Start "detecting" darts
		/// </summary>
		public void Start()
		{
			detectDart();
			timer.Start();
		}
		
		/// <summary>
		/// Stop detecting darts
		/// </summary>
		public void Stop()
		{
			timer.Stop();
		}
		
		/// <summary>
		/// Show the interface
		/// </summary>
		public void ShowInterface()
		{
			setup = new Setup(timer.Interval);
			setup.SetDesktopLocation(100, 100);

			DialogResult r = setup.ShowDialog();
			if(r == DialogResult.OK)
			{
				timer.Interval = setup.delay;
			}
			setup.Dispose();
		}
		
		/// <summary>
		/// Hide the interface
		/// </summary>
		public void HideInterface()
		{
			
		}
	}
}
